package com.ug_project.adapters

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import android.view.ViewGroup
import com.ug_project.objects.isNotNull
import com.ug_project.objects.isNull



/**
 * Created by Administrator on 2/19/2017.
 */

open class FragmentPagerAdapter(var fm: FragmentManager) : FragmentPagerAdapter(fm)
{
    private var _getItem: ((position: Int) -> Fragment)? = null
    private var _getCount: (() -> Int)? = null

    override fun getItem(position: Int): Fragment {
        var item = Fragment()
        _getItem isNull {throw Exception("پارامىتىر قۇرۇق بولۇپ قالسا بولمايدۇ") }
        _getItem isNotNull { item = _getItem!!(position) }
        return item
    }

    override fun getCount(): Int {
        var item = -1
        _getCount isNull {throw Exception("پارامىتىر قۇرۇق بولۇپ قالسا بولمايدۇ") }
        _getCount isNotNull { item = _getCount!!() }
        return item
    }
    open fun GetItem(getitem: (position: Int) -> Fragment)
    {
        _getItem = getitem
    }
    open fun GetCount(getcount: () -> Int)
    {
        _getCount = getcount
    }


    var mChildCount=0
    override fun notifyDataSetChanged() {
        mChildCount = count
        super.notifyDataSetChanged()
    }

    override fun getItemPosition(item: Any): Int {
        if(mChildCount>0){
            mChildCount--
            return POSITION_NONE
        }else{
            return super.getItemPosition(item)
        }
    }


}

fun createFragmentPagerAdapter(fm: FragmentManager, constructor: com.ug_project.adapters.FragmentPagerAdapter.() -> Unit) : com.ug_project.adapters.FragmentPagerAdapter
{
    val item = com.ug_project.adapters.FragmentPagerAdapter(fm)
    item.constructor()
    return item
}